package me.leiusual.sharing.ui.order

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import com.google.gson.JsonObject
import me.leiusual.sharing.api.model.orderdetail.OrderDetail
import me.leiusual.sharing.repository.OrderRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.base.AbstractViewModel
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-31
 * description:
 */
class OrderDetailViewModel @Inject constructor(
    private val orderRepository: OrderRepository
) : AbstractViewModel() {

  // 订单详情

  private val _orderDetail = MutableLiveData<ParamsOrderDetail>()
  val orderDetail: LiveData<Data<OrderDetail>> = switchMap(_orderDetail) {
    orderRepository.orderDetail(it.storeId, it.code).toLiveData()
  }

  fun orderDetail(storeId: Long, code: String) {
    _orderDetail.value = ParamsOrderDetail(storeId, code)
  }

  data class ParamsOrderDetail(val storeId: Long, val code: String)

  // 继续支付

  private val _orderRePay = MutableLiveData<String>()
  val orderRePay: LiveData<Data<JsonObject>> = switchMap(_orderRePay) {
    orderRepository.orderRePay(it).toLiveData()
  }

  fun orderRePay(code: String) {
    _orderRePay.value = code
  }

  // 确认收货

  private val _orderConfirm = MutableLiveData<String>()
  val orderConfirm: LiveData<Data<Unit>> = switchMap(_orderConfirm) {
    orderRepository.orderConfirm(it).toLiveData()
  }

  fun orderConfirm(code: String) {
    _orderConfirm.value = code
  }

  // 删除订单

  private val _orderDelete = MutableLiveData<String>()
  val orderDelete: LiveData<Data<Unit>> = switchMap(_orderDelete) {
    orderRepository.orderDelete(it).toLiveData()
  }

  fun orderDelete(code: String) {
    _orderDelete.value = code
  }
}